﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
//using _180FrameWork.Common;
//using _180FrameWork.Common.Models;
using _180FrameWork.CustControls.Models;

namespace _180FrameWork.CustControls
{
    public partial class CustDataGridView : DataGridView
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CustDataGridView()
        {
            InitializeComponent();
            //InitStyle();
        }
        public CustDataGridView(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        #region 样式
        /// <summary>
        /// 是否保存样式
        /// </summary>
        [DefaultValue(false)]
        public bool SaveStyle
        {
            get;
            set;
        }

        /// <summary>
        /// 控件唯一标识符
        /// </summary>
        public string GUID
        {
            get;
            set;
        }
        ///// <summary>
        ///// 样式
        ///// </summary>
        //private CustDataGridViewStyle _Style;
        ///// <summary>
        ///// 清理样式
        ///// </summary>
        //private void CleanStyle()
        //{
        //    this._Style = new CustDataGridViewStyle();
        //    this._Style.DataGridViewName = this.GUID;
        //    foreach (DataGridViewColumn column in this.Columns)
        //    {
        //        this._Style.ColumnStyleList.Add(new CustDataGridViewColumnStyle()
        //        {
        //            DisplayIndex = column.Index,
        //            Name = column.Name,
        //            DataPropertyName = column.DataPropertyName,
        //            HeaderText = column.HeaderText,
        //            Visible = true,
        //            Width = 100
        //        });
        //    }
        //    CustDataGridViewStyleHelper.SaveStyle(_Style);
        //}
        ///// <summary>
        ///// 初始化样式
        ///// </summary>
        //public void InitStyle()
        //{
        //    if (this.SaveStyle)
        //    {
        //        if (this._Style == null)
        //        {
        //            var s = CustDataGridViewStyleHelper.GetStyle(this.GUID);
        //            if (s == null)
        //            {
        //                CleanStyle();
        //            }
        //            else
        //            {
        //                this._Style = s;
        //            }
        //        }
        //        InitColumnStyle(_Style);
        //        this.ColumnWidthChanged += new System.Windows.Forms.DataGridViewColumnEventHandler(this.CustDataGridView_ColumnWidthChanged);
        //    }
        //}
        ///// <summary>
        ///// 初始化列样式
        ///// </summary>
        ///// <param name="style"></param>
        //private void InitColumnStyle(CustDataGridViewStyle style)
        //{
        //    try
        //    {
        //        foreach (var item in style.ColumnStyleList)
        //        {
        //            if (this.Columns.Contains(item.Name))
        //            {
        //                var column = this.Columns[item.Name];
        //                column.Visible = item.Visible;
        //                column.DisplayIndex = item.DisplayIndex;
        //                column.Width = item.Width;
        //            }
        //        }
        //        this.Refresh();
        //    }
        //    catch
        //    {
        //        CleanStyle();
        //    }
        //}
        ///// <summary>
        ///// 打开样式窗口
        ///// </summary>
        //public void OpenChangeColumnStyleDialog()
        //{
        //    if (this.SaveStyle)
        //    {
        //        frmDataGridViewColumnSetting frm = new frmDataGridViewColumnSetting(_Style);
        //        if (frm.ShowDialog() == DialogResult.OK)
        //        {
        //            if (frm.option == "reload")
        //                CleanStyle();
        //            InitColumnStyle(_Style);
        //        }
        //    }
        //}
        ///// <summary>
        ///// 列宽变更后触发样式变更事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void CustDataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        //{
        //    if (this.SaveStyle)
        //    {
        //        foreach (var ColumnStyle in _Style.ColumnStyleList)
        //        {
        //            if (ColumnStyle.Name == e.Column.Name)
        //                ColumnStyle.Width = e.Column.Width;
        //        }
        //    }
        //}
    
        #endregion

        #region 数据
        ///// <summary>
        ///// 是否已经绑定数据
        ///// </summary>
        //public bool IsDataBindingComplete { get { return this._IsDataBindingComplete; } }
        //private bool _IsDataBindingComplete = false;

        //private void MyDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        //{
        //    this._IsDataBindingComplete = true;
        //}

        //private void bs_DataSourceChanged(object sender, EventArgs e)
        //{
        //    this.OnDataSourceChanged(e);
        //}

        //private void bs_CurrentItemChanged(object sender, EventArgs e)
        //{
        //}

        ////
        //// 摘要:
        ////     当基础列表更改或列表中的项更改时发生。
        //[Category("Data")]
        //[Description("BindingSourceListChangedEventHandlerDescr")]
        //public event ListChangedEventHandler ListChanged;

        //// 执行事件
        //protected virtual void OnListChanged(ListChangedEventArgs e)
        //{
        //    if (ListChanged != null)
        //    {
        //        ListChanged(this, e);
        //    }
        //}

        //private void bs_ListChanged(object sender, ListChangedEventArgs e)
        //{
        //    this.OnListChanged(e);
        //}

        //private void bs_DataMemberChanged(object sender, EventArgs e)
        //{

        //}

        #endregion

        #region 行变更
        /// <summary>
        /// 当前选中行
        /// </summary>
        public int CurrentRowIndex { get { return this._CurrentRowIndex; } }
        private int _CurrentRowIndex = -1;

        /// <summary>
        /// 当前选择行出现变更
        /// </summary>
        [Category("Data")]
        [Description("DataGridView_SelectionRowChangedDescr")]
        public event DataGridViewRowEventHandler SelectionRowChanged;

        /// <summary>
        /// 执行行变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnSelectionRowChanged(object sender, DataGridViewRowEventArgs e)
        {
            if (SelectionRowChanged != null)
            {
                SelectionRowChanged(this, e);
            }
        }
        /// <summary>
        /// 触发行变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (this.CurrentCell != null && this.CurrentRow != null)
            {
                if (CurrentRowIndex != this.CurrentRow.Index)
                {
                    _CurrentRowIndex = this.CurrentRow.Index;
                    DataGridViewRowEventArgs eArgs = new DataGridViewRowEventArgs(this.CurrentRow);
                    OnSelectionRowChanged(this, eArgs);
                }
            }
        }

        #endregion        
        
        #region 导出
        /// <summary>
        /// 导出CSV格式excel
        /// </summary>
        public void ExportCSV()
        {
            //MyExcelHelper.DataGridViewExportToCSV(this);
        }
        /// <summary>
        /// 导出html格式excel
        /// </summary>
        public void ExportHTML()
        {
            //MyExcelHelper.DataGridViewExportToHTML(this);
        }
        /// <summary>
        /// 导出EXCEL数据
        /// </summary>
        public void ExportExcel()
        {
            //MyExcelHelper.DataGridViewExportToXLS(this);
        }

        //public void tsExportCSV_Click(object sender, EventArgs e)
        //{
        //    this.ExportCSV();
        //}
        //public void tsExportEXCEL_Click(object sender, EventArgs e)
        //{
        //    this.ExportExcel();
        //}
        //public void tsExportHTML_Click(object sender, EventArgs e)
        //{
        //    this.ExportHTML();
        //}
        #endregion

        #region 行号
        /// <summary>
        /// 自定义绘制行事件中显示行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (ShowRowIndex)
            {
                Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                    e.RowBounds.Location.Y,
                    this.RowHeadersWidth - 4,
                    e.RowBounds.Height);

                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                    this.RowHeadersDefaultCellStyle.Font,
                    rectangle,
                    this.RowHeadersDefaultCellStyle.ForeColor,
                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
        }
        /// <summary>
        /// 显示行号
        /// </summary>
        public bool ShowRowIndex { get; set; }
        #endregion

        #region 无效
        private void MyWinDataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            OnAddingNew(this, e);
        }

        public event DataGridViewRowEventHandler AddingNew;

        // 执行事件
        protected virtual void OnAddingNew(object sender, DataGridViewRowEventArgs e)
        {
            if (AddingNew != null)
            {
                AddingNew(this, e);
            }
        }

        private void MyWinDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (this.Rows[e.RowIndex].IsNewRow)
                return;
        }
        [Category("Data")]
        [Description("TextBoxButtonClickEventHandlerDescr")]
        public event DataGridViewCellEventHandler TextBoxButtonClicked;

        public void CellButtonClick(int CIndex, int RIndex)
        {
            if (TextBoxButtonClicked != null)
            {
                TextBoxButtonClicked(this, new DataGridViewCellEventArgs(CIndex, RIndex));
            }
        }

        #endregion

        #region 筛选条件
        /// <summary>
        /// 筛选条件列表
        /// </summary>
        public IList<CustQueryCondition> QCList
        {
            get
            {
                return this._qclist;
            }
        }
        private IList<CustQueryCondition> _qclist = new List<CustQueryCondition>();
        
        /// <summary>
        /// 打开筛选条件窗口
        /// </summary>
        public void OpenSetFilterDialog()
        {
            if (this.DataSource != null)
            {
                frmDataGridViewQuery frm = new frmDataGridViewQuery(this);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    QCList.Add(frm.QC);
                    OnQCListChanged(this, new EventArgs());
                    if (this.qp != null)
                    {
                        qp.AddCustQueryCondition(frm.QC.Key, frm.QC.DisPlay);
                    }
                }
            }
        }
        private CustQueryPanel _qp;
        public CustQueryPanel qp
        {
            get { return this._qp; }
            set
            {
                this._qp = value;
                if (this._qp != null)
                {
                    this._qp.QCAdded += new EventHandler(_qp_QCAdded);
                    this._qp.QCRemoveed += new EventHandler(_qp_QCRemoveed);
                }
            }
        }        

        void _qp_QCAdded(object sender, EventArgs e)
        {
        }

        void _qp_QCRemoveed(object sender, EventArgs e)
        {
            var lb = sender as CustQueryLabel;
            foreach (var item in QCList)
            {
                if (item.Key == lb.Name)
                {
                    QCList.Remove(item);
                    OnQCListChanged(this, new EventArgs());
                    break;
                }
            }
        }

        /// <summary>
        /// 查询条件变更的时候
        /// </summary>
        [Category("Data")]
        [Description("CustDataGridView_QCListChanged")]
        public event EventHandler QCListChanged;

        // 执行事件
        protected virtual void OnQCListChanged(object sender, EventArgs e)
        {
            if (QCListChanged != null)
            {
                QCListChanged(this, e);
            }
        }
        #endregion
    }
}
